---@class UIFriendGift : UIBase
UIFriendGift = UIBase.New(PanelResNames.UIFriendGift)
local t = UIFriendGift

function t.Init()
    UIBase.Init(t)
end

function t:OnCreate()
    self.btnAllGet = self:GetChild("btnAllGet")
    self.btnDelete = self:GetChild("btnDelete")
    self.btnGiftBox = self:GetChild("btnGiftBox")
    self.giftBoxLight = self:GetChild("light",self.btnGiftBox)
    self.boxPointIcon = self:GetChild("boxPointIcon/icon")
    ---@type GList
    self.itemList = self:GetChild("list")
    self.btnInfo = self:GetChild("btnInfo")
    ---@type GProgressBar
    self.boxBar = self:GetChild("boxBar")
    self.expBar = self:GetChild("expBar")
    self.btnExpBox = self:GetChild("btnExpBox")
    self.expLight = self:GetChild("light",self.btnExpBox)

    self.txtLevel = self:GetChild("level")
    self.txtTitle = self:GetChild("title")
    self.info = self:GetChild("info")
    self.boxInfo = self:GetChild("boxInfo")
    self.btnCloseInfo = self:GetChild("btnCloseInfo")
    self.ctrlPage = self:GetController("ctrlPage")
    self.ctrlDialog = self:GetController("ctrlDialog")
    --self.ctrlDes = self:GetController("ctrlDes")
    self.btnDialogSure = self:GetChild("dialog/btnDelete")
    self.btnDialogCancel = self:GetChild("dialog/btnCancel")
    self.ctrlNothing = self:GetController("nothing")
    self.ctrlRewardTips = self:GetController("ifShowReward")
    self.rewardTips = self:GetChild("rewardTips/btnCloseReward")
    self.rewardShowList = self:GetList("rewardTips/itemGrid")
    self:RegisterOnClick(self.btnAllGet,self.ClickAllGet)
    self:RegisterOnClick(self.btnDelete,self.ClickDelete)
    self:RegisterOnClick(self.btnGiftBox,self.ClickGiftBox)
    self:RegisterOnClick(self.btnExpBox,self.ClickExpBox)
    self:RegisterOnClick(self.btnInfo,self.ClickInfo)
    self:RegisterOnClick(self.btnDialogSure,self.ClickDialogSure)
    self:RegisterOnClick(self.btnDialogCancel,self.ClickDialogCancel)
    self:RegisterOnClick(self.btnCloseInfo,self.ClickCloseInfo)
    self:RegisterOnClick(self.rewardTips,self.ClickCloseReward)
    self:RegisterOnChanged(self.ctrlPage,self.OnPageChanged)
    self:SetDynamicList(self.itemList,self.ItemRender)
    self:SetDynamicList(self.rewardShowList,self.RewardShowItemRender,nil,true)
    self:RegisterEvent(Config.EventType.Fresh_FriendGiftInfo,self.RefreshFriendGiftInfoMsg)
    self:RegisterEvent(Config.EventType.Refresh_FriendSocialLevel,self.RefreshSocialLevel)
    self.poolMgr = ComponentPoolMgr.New(t.FriendGiftItem)
    self.dataList = {}
    self.showRewardDataList = {}
    self.info.text = getLanguage("HelpDes11")
    self.boxInfo.text = getLanguage("HelpDes10")
    friendHandler.ReqAllFriendGiftList()

end
function t:Refresh()
    self:DoChange()
    self:RefreshSocialLevel()
    --大宝箱
    local cfg = logicMgr.FriendManager.GetBigBoxCfg()
    local cur = logicMgr.FriendManager.friendGiftPoint
    local max = cfg.f_FriendyKey

    if cfg then
        self.boxBar.max = max
        self.boxBar.value = cur
        self:SetIconById(self.boxPointIcon,CurrencyEnum.FriendGiftBoxPoint,EnumConst.ItemShowType.ITEM)
        self.txtTitle.text = getLanguage(cfg.f_ItemName)

    else
        logError("can't find cfg ")
    end
    if(cfg and cur >= max)then
        self.btnGiftBox:GetController("reward").selectedIndex=1
    else
        self.btnGiftBox:GetController("reward").selectedIndex=0
    end

end
function t:RefreshSocialLevel()
    local level = logicMgr.FriendManager.friendGiftLevel
    local cfg = dataTable.getDataTable("t_friendylvreward")
    local data = cfg[level]
    self.txtLevel.text = string.format(getLanguage("Sys_Friend_Gift_Social_Level"),level)
    if data then
        local expMax = math.max(data.f_FriendyPoint,1)
        self.expBar.value = math.min(logicMgr.FriendManager.friendGiftExp,expMax)
        self.expBar.max = expMax
    else
        self.expBar.value = 0
    end
    if(data and level > logicMgr.FriendManager.friendGiftSocialBoxLevel)then
        self.btnExpBox:GetController("reward").selectedIndex=1
    else
        self.btnExpBox:GetController("reward").selectedIndex=0
    end
end
function t:OnEnable()

    --[[self.ctrlDes.selectedIndex = 0
    local flag1 = logicMgr.FriendManager.HasReward(1)
    local flag2 = logicMgr.FriendManager.HasReward(2)
    --local hasRare = logicMgr.FriendManager.HasRareList()
    if flag2 or (not flag1)  then
        self.ctrlPage.selectedIndex = 1
    else
        self.ctrlPage.selectedIndex = 0
    end]]
    self:Refresh()
end
function t:OnDisable()
    if self.poolMgr then
        self.poolMgr:OnDisable()
    end
    self:RemoveTimeCall(self.DelayReSort)
end
function t:OnDestroy()
    self.poolMgr:OnDestroy()
    self.poolMgr = nil
    self.dataList = nil
    self.showRewardDataList = nil
end
function t:ItemRender(index,go)
    ---@type FriendGiftItem
    local item = self.poolMgr:GetBindScript(go)
    item:Refresh(self.dataList[index],self)
end
function t:RewardShowItemRender(index,go)
    local data = self.showRewardDataList[index]
    local item = CtrlManager.GetCtrl(PanelNames.Item)
    item.initData(go,data)
end
function t:ClickAllGet()
    local time = HelperFunc.GetServerTime()
    local flag = false
    for k,v in pairs(self.dataList) do
        if not v.isGet and v.endTime > time then
            flag = true
            break
        end
    end
    if flag then
        friendHandler.ReqGetFriendGift(0)
    else
        UISysTips.AddMsg(getLanguage("Sys_Friend_Gift_No_Gift_Get"))
    end
end
function t:ClickDelete()
    self.ctrlDialog.selectedIndex = 0
    local time = HelperFunc.GetServerTime()
    local hasDelete = false
    for k,v in pairs(self.dataList) do
        if  v.isGet or v.endTime <= time then
            hasDelete = true
            break
        end
    end
    if hasDelete then
        self.ctrlDialog.selectedIndex = 1
    end
end
--终极宝箱
function t:ClickGiftBox()
    if self.boxBar.value < self.boxBar.max then
        --self.ctrlDes.selectedIndex = 2
        local box = logicMgr.FriendManager.GetBigBoxCfg()
        if box then
            self:ShowRewardView(box.f_FriendyBoxReward)
        end
    else
        friendHandler.ReqGetFriendGiftBigBox()
    end
end
function t:ShowRewardView2(list)
    table.clear(self.showRewardDataList)
    for k,v in pairs(list) do
        table.insert(self.showRewardDataList,{itemModelId = v[1],num = v[2]})
    end
    self.ctrlRewardTips.selectedIndex = 1
    self.rewardShowList.numItems = #self.showRewardDataList
end
function t:ShowRewardView(rewardJson)
    local list = decodeJsonStrToTabel(rewardJson)
    self:ShowRewardView2(list)
end
--社交等级宝箱
function t:ClickExpBox()
    if logicMgr.FriendManager.friendGiftLevel > logicMgr.FriendManager.friendGiftSocialBoxLevel then
        friendHandler.ReqGetFriendGiftSocialBox()
    else
        local cfg = dataTable.getDataTableById("t_friendylvreward",FriendManager.friendGiftLevel)
        if not cfg then return end
        self:ShowRewardView(cfg.f_FriendyReward)
    end
end
function t:ClickCloseReward()
    self.ctrlRewardTips.selectedIndex = 0
end
function t:ClickInfo()
    logicMgr.UIShowManager.ShowCommonTips("Friend_Title_Gift","HelpDes43", "FunctionStory9002")
end
function t:ClickDialogSure()
    self.ctrlDialog.selectedIndex = 0
    friendHandler.ReqDeleteFriendGift()
end
function t:ClickDialogCancel()
    self.ctrlDialog.selectedIndex = 0
end
function t:ClickCloseInfo()
    --self.ctrlDes.selectedIndex = 0
end
function t:OnPageChanged()
    self:DoChange(true)
end
function t:DoChange(isTop)
    local index = self.ctrlPage.selectedIndex + 1
    logicMgr.FriendManager.GetFriendGiftList(index,self.dataList)
    self.btnAllGet.visible = index == 1
    self:DelayReSort(isTop)
end
function t:RefreshList(isTop)
    table.sort(self.dataList,t.SortFriendGift)
    self.poolMgr:ReturnUsedToPool()
    self.itemList.numItems = #self.dataList
    if isTop then
        self.itemList:ScrollToView(0,false,true)
    end
    if #self.dataList == 0 then
        self.ctrlNothing.selectedIndex = 1
    else
        self.ctrlNothing.selectedIndex = 0
    end
end
function t:DelayReSort(isTop)
    local time = HelperFunc.GetServerTime()
    local min = 10000000
    for k,v in pairs(self.dataList) do
        if not v.isGet and v.endTime > time then
            min = math.min(min,v.endTime - time)
        end
    end
    self:RefreshList(isTop)
    self:RemoveTimeCall(self.DelayReSort)
    if min > 0 and min ~= 10000000 then
        self:TimeCall(self.DelayReSort,min* 1000 + 100,1)
    end
end
---@param a ClientFriendGift
---@param b ClientFriendGift
function t.SortFriendGift(a,b)
    local wa = 0
    local wb = 0
    local time = HelperFunc.GetServerTime()
    --稀有 普通
    wa = a.f_Type
    wb = a.f_Type
    if wa ~= wb then
        return wa > wb
    end
    --已领取 已过期
    wa = a.isGet and 0 or 1
    if not a.isGet then
        wa = a.endTime < time and 1 or 0
    end
    wb = b.isGet and 0 or 1
    if not b.isGet then
        wb = b.endTime < time and 1 or 0
    end
    if wa ~= wb then
        return wa < wb
    end

    --可领取
    wa = a.isGet and 1 or 0
    wb = b.isGet and 1 or 0
    if wa ~= wb then
        return wa < wb
    end

    --过期
    wa = a.endTime > time and 1 or 0
    wb = b.endTime > time and 1 or 0
    if wa ~= wb then
        return wa > wb
    end
    if a.endTime ~= b.endTime then
        return a.endTime > b.endTime
    end
    return a.id < b.id
end

function t:RefreshFriendGiftInfoMsg()
    self:Refresh()
end

--item控件
t.FriendGiftItem = Class("FriendGiftItem",UIComponentInterface)
---@class FriendGiftItem: UIComponentInterface
local t_t = t.FriendGiftItem
function t_t:OnCreate(obj)
    self.txtTitle = self:GetChild("title")
    self.txtDes = self:GetChild("des")
    self.txtTime = self:GetChild("time")
    self.btnGet = self:GetChild("btnGet")
    self.item = self:GetChild("item")
    self.ctrlState = self:GetController("ctrlState")
    self.ctrlType = self:GetController("type")
    self.serial = self:GetChild("resouse")
    self.rewardItem = {}
    for i = 1,2 do
        local temp = {
            icon = self:GetChild("icon"..i),
            num = self:GetChild("num"..i)
        }
        table.insert(self.rewardItem,temp)
    end
    self.boxIcon = self:GetChild("boxIcon")
    self:RegisterOnClick(self.boxIcon,self.ClickIcon)
    self:RegisterOnClick(self.btnGet,self.ClickGet)
end
---@param data ClientFriendGift
---@param parent UIFriendGift
function t_t:Refresh(data,parent)
    self.data = data
    self.parent = parent
    local cfg = dataTable.getDataTableById("t_friendygift",data.modelId)
    self.txtTitle.text= getLanguage( cfg.f_FriendyGiftName)
    self.txtDes.text = string.format(getLanguage(cfg.f_FriendyGiftDescribe),data.playerName)
    self.ctrlType.selectedIndex = cfg.f_Type == 1 and 0 or 1
    self.serial.visible = true;
    self:RemoveTimeCall(self.UpdateTime)
    if self.data.isGet then
        self.ctrlState.selectedIndex = 1    --已领取
        self:RefreshReward()
    else
        local cd = self.data.endTime - HelperFunc.GetServerTime()
        if cd <= 0 then
            self.ctrlState.selectedIndex = 2    --过期
        else
            self.ctrlState.selectedIndex = 0   --未领取
            self:TimeCall(self.UpdateTime,1000,-1)
            self:UpdateTime()
        end
    end
    if not self.maxTB then
        self.maxTB = decodeJsonStrToTabel(dataTable.getGlobalString(199))
    end
    local max = self.maxTB[cfg.f_TableType]
    if max and max <=0 then
        self.serial.text = tostring(self.data.serial)
    else
        self.serial.text = string.format("%d/%d",self.data.serial,max)
    end
    local items = decodeJsonStrToTabel(cfg.f_Reward)
    logicMgr.CostManager.RefreshItem(self.item, items[1]);
end
function t_t:RefreshReward()
    local cfg = dataTable.getDataTableById("t_friendygift",self.data.modelId)
    if cfg then
        self:SetIconById(self.rewardItem[1].icon,CurrencyEnum.FriendGiftSocialPoint,EnumConst.ItemShowType.ITEM)
        self.rewardItem[1].num.text = cfg.f_CommunityIntersection
        self:SetIconById(self.rewardItem[2].icon,CurrencyEnum.FriendGiftBoxPoint,EnumConst.ItemShowType.ITEM)
        self.rewardItem[2].num.text = cfg.f_KeyPoint
    end

end
function t_t:UpdateTime()
    local cd = self.data.endTime - HelperFunc.GetServerTime()
    self.txtTime.text = HelperFunc.GetTimeStr(cd)
    if cd <= 0 then
        self.ctrlState.selectedIndex = 2    --过期
        self:RemoveTimeCall(self.UpdateTime)
    end
end
function t_t:OnDisable()
    self:RemoveTimeCall(self.UpdateTime)
end
function t_t:ClickIcon()
    local cfg = dataTable.getDataTableById("t_friendygift",self.data.modelId)
    if not cfg then return end
    local list = {}
    local list1 = decodeJsonStrToTabel(cfg.f_Reward)
    for k,v in pairs(list1) do
        table.insert(list,v)
    end
    table.insert(list,{CurrencyEnum.FriendGiftSocialPoint, cfg.f_CommunityIntersection})
    table.insert(list,{CurrencyEnum.FriendGiftBoxPoint, cfg.f_KeyPoint})
    self.parent:ShowRewardView2(list)
end
function t_t:ClickGet()
    friendHandler.ReqGetFriendGift(self.data.id)
end

function t_t:OnDestroy()

end



return t